﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PlcUI_test.MyControls
{
    [Description("焊机数据写入控件")]
    public partial class InputBoxButton : UserControl
    {
        /// <summary>
        /// 数值下界
        /// </summary>
        private double lowerLimit = 0.0;
        /// <summary>
        /// 数值上界
        /// </summary>
        private double upperLimit = 0.0;
        /// <summary>
        /// 输入框对应焊机属性的名称
        /// </summary>
        private string boxText;
        /// <summary>
        /// 转换出来的值
        /// </summary>
        private double value;



        [Description("数值的下界")]
        public double LowerLimit
        { get { return lowerLimit; } set { lowerLimit = value; SetText(); } }
        [Description("数值的上界")]
        public double UpperLimit
        { get { return upperLimit; } set { upperLimit = value; SetText(); } }
        [Description("文本框对应焊机属性的名称")]
        public string ValueName
        { get { return boxText; } set { boxText = value; } }

        public InputBoxButton()
        {
            InitializeComponent();
            SetText();
        }
        public void SetText()
        {
            string temp = LowerLimit.ToString() + "—" + UpperLimit.ToString();
            label1.Text = temp;
        }

        public void Initialize()
        {

        }

        [Description("按钮按下后触发")]
        public event Action<double> ButtonClicked;

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                double temp = double.Parse(textBox1.Text);
                try
                {
                    if (temp >= LowerLimit && temp <= UpperLimit)
                    {
                        value = temp;
                        textBox1.Clear();
                        ButtonClicked?.Invoke(value);
                    }
                    else
                    {
                        throw new Exception();
                    }
                }
                catch (Exception)
                {
                    MessageBox.Show("输入的值超出范围！");
                    textBox1.Clear();
                }
            }
            catch (Exception)
            {
                MessageBox.Show("输入的字符不能转为数字！");
                textBox1.Clear();
            }
        }
    }
}
